package com.hnu.store.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;

public class JwtUtils {

  //  过期时间:7天
  private static final long expire = 604800;
  //  32位密钥
  private static final String secret = "hjcdlwbwcztdfhnunfttradingplatfo";

  //  生成token
  public static String generateToken(String username) {
    //过期时间
    Date now = new Date();
    Date expiration = new Date(now.getTime() + expire * 1000);

    return Jwts.builder()
        .setHeaderParam("type", "JWT")
        .setSubject(username)
        //  生效时间
        .setIssuedAt(now)
        .setExpiration(expiration)
        .signWith(SignatureAlgorithm.HS512, secret)
        .compact();
  }

  //  验证，对token进行解析
  public static Claims getClaimsByToken(String token) {
    return Jwts.parser()
        .setSigningKey(secret)
        .parseClaimsJws(token)
        .getBody();
  }
}
